﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.DataContract;
using MaxLeaf.Platform.DALImplementation;
using CommonsLib.DB;
using MaxLeaf.Platform.Functions;
using System.Data.SqlClient;

namespace MaxLeaf.Platform.BRL
{
    public class mUser_MainBRL:BaseBRL
    {

        public override string UniqueFields
        {
            get
            {
                return "UserName";
            }
        }

        public override bool AddValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!base.AddValid(dbcon, dal, model))
            {
                return false;
            }

            if (!Checkis_LocAppro(dbcon, dal, model))
            {
                return false;
            }
             

            return true;
        }

        public override bool UpdateValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!base.UpdateValid(dbcon, dal, model))
            {
                return false;
            }

            if (!Checkis_LocAppro(dbcon, dal, model))
            {
                return false;
            }
             

            return true;
        }

        private bool Checkis_LocAppro(DBAccess dbcon, BaseDAL dal, object model)
        {
            
            mUser_MainDataModel user = (mUser_MainDataModel)model;
            if (!user.is_LocAppro) return true;

            string strSql = "select top 1 * from mUser_Main  where UserID<>@UserID and CompanyID=@CompanyID and is_LocAppro=1";
            using (System.Data.Common.DbDataReader dr = dbcon.GetDataFirstRow(strSql, new SqlParameter[]
                {
                    new SqlParameter("@UserID",user.Key)
                    ,new SqlParameter("@CompanyID",user.CompanyID)
                    
                }))
            {
                if (dr.Read())
                {
                    throw new MaxLeaf.Platform.Commons.MaxLeafException("已存在库存审核人");
                }
            }

            return true;
        }
         
    }
}
